//
//  HYSeatBookingVC.m
//  Booking
//
//  Created by Duntech on 2018/4/19.
//  Copyright © 2018年 Duntech. All rights reserved.
//

#import "HYSeatBookingVC.h"
#import "HYSeatBookingDetailVC.h"
#import "HYBookingRoomFilterView.h"
#import "HYSeatListCell.h"
#import "HYSeatCellModel.h"
#import "HYDateCitySelectView.h"
#import "HYBookSeatPlaneVC.h"
#import "HYCityModel.h"

@interface HYSeatBookingVC ()

/** dataSource */
@property (nonatomic,strong) HYTableViewDataSource *dataSource;
/** delegate */
@property (nonatomic,strong) HYTableViewDelegate *delegate;
/** dataSets数据源 */
@property (nonatomic,strong) NSMutableArray *dataSetsArray;
@property (nonatomic,strong) NSMutableArray *sectionArray;
@property (nonatomic,strong) HYBookingRoomFilterView *filterView;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) HYDateCitySelectView *dateCitySelectView;
@property (nonatomic,assign) BOOL isShowSelectView;

@property (nonatomic,copy)   NSString *selectCityID;
@property (nonatomic,copy)   NSString *selectBuildingID;
@property (nonatomic,copy)   NSString *selectStartTime;
@property (nonatomic,copy)   NSString *selectEndTime;
@property (nonatomic,copy)   NSString *status;

@end

@implementation HYSeatBookingVC

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self setupSubViews];
    [self setupTableViewCellBinding];
}

-(void)setupNav{
    [super setupNav];
}

- (void)setupSubViews{
    
    self.title = NSLocalizedString(@"bookingSeat", nil);
    self.dataSource.dataSource = self.dataSetsArray;
    self.delegate.dataSource = self.dataSetsArray;
    [self.view addSubview:self.filterView];
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[HYSeatListCell class] forCellReuseIdentifier:@"HYSeatListCell"];
    
    [_filterView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.view);
        make.height.mas_equalTo(KFilterViewHeight);
    }];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.view);
        make.top.mas_equalTo(self.filterView.mas_bottom);
    }];
}

- (void)setupTableViewCellBinding{
    
    kWeakSelf(weakSelf);
    [self.dataSource setBindingCellBlock:^(UITableView *tableView, id rowDataSource, HYBaseTableViewCell *cell, NSIndexPath *indexPath) {
        
    }];
    
    [self.delegate setDidSelectedBlock:^(UITableView *tableView, NSIndexPath *indexPath) {
        
        HYBookSeatPlaneVC *planeVC = [[HYBookSeatPlaneVC alloc] init];
        [planeVC.dataSetsArray addObjectsFromArray:weakSelf.sectionArray];
        planeVC.meetingDate = [weakSelf.selectStartTime substringToIndex:10];
        planeVC.startTime = [weakSelf.selectStartTime substringWithRange:NSMakeRange(11, 5)];
        planeVC.endTime = [weakSelf.selectEndTime substringWithRange:NSMakeRange(11, 5)];
        [weakSelf.navigationController pushViewController:planeVC animated:NO];
        // 设置翻页动画为从右边翻上来
        [UIView transitionWithView:weakSelf.navigationController.view duration:1 options:UIViewAnimationOptionTransitionFlipFromRight animations:nil completion:nil];
    }];
}

- (void)requestNetwork{
    
    kWeakSelf(weakSelf);
    [HYNetRequestHandle getAvailableSeatListWithPageNo:nil pageSize:nil startTime:self.selectStartTime endTime:self.selectEndTime status:self.status cityId:self.selectCityID buildId:self.selectBuildingID floorId:nil Complection:^(NSArray *AvailableSeatList) {
        [weakSelf.sectionArray removeAllObjects];
        [weakSelf.dataSetsArray removeAllObjects];
        for (NSDictionary *dict in AvailableSeatList) {
            
            HYSeatCellModel *cellModel = [HYSeatCellModel modelWithDictionary:dict];
            cellModel.cellHeight = 108;
            cellModel.cellIdientifier = @"HYSeatListCell";
            [weakSelf.sectionArray addObject:cellModel];
        }
        weakSelf.filterView.roomNumber = @(AvailableSeatList.count).stringValue;
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.dataSetsArray addObject:weakSelf.sectionArray];
        [weakSelf.tableView reloadData];
    }];
}

#pragma mark - action
- (void)tapFilterViewAction{
    
    if (!self.isShowSelectView) {
        
        [self.dateCitySelectView showDateCitySelectViewInView:self.view];
    }
    else{
        [self.dateCitySelectView hideDateCitySelectView];
    }
    self.isShowSelectView = !self.isShowSelectView;
}

#pragma mark - emptyDelegate
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
    
    if ([HttpManager shareHTTPManager].reachabilityManager.networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable || [HttpManager shareHTTPManager].reachabilityManager.networkReachabilityStatus == AFNetworkReachabilityStatusUnknown) {
        
        return [UIImage imageNamed:@"icon_wifi"];
    }
    return [UIImage imageNamed:@"no_seat"];
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{
    
    NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:@"The internet is lost" attributes:@{NSFontAttributeName : KFitFont(16),NSForegroundColorAttributeName : KAPP_BLACK_COLOR}];
    if ([HttpManager shareHTTPManager].reachabilityManager.networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable || [HttpManager shareHTTPManager].reachabilityManager.networkReachabilityStatus == AFNetworkReachabilityStatusUnknown) {
        
        return attributeStr;
    }
    return [[NSAttributedString alloc] initWithString:NSLocalizedString(@"noSeatsFound", nil)];;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{
    return YES;
}

#pragma mark - lazyload
- (HYTableViewDataSource *)dataSource{
    
    if (!_dataSource) {
        _dataSource = [HYTableViewDataSource new];
    }
    return _dataSource;
}

- (NSMutableArray *)dataSetsArray{
    
    if (!_dataSetsArray) {
        _dataSetsArray = [NSMutableArray array];
    }
    return _dataSetsArray;
}

- (NSMutableArray *)sectionArray{
    
    if (!_sectionArray) {
        _sectionArray = [NSMutableArray array];
    }
    return _sectionArray;
}

- (HYTableViewDelegate *)delegate{
    
    if (!_delegate) {
        _delegate = [HYTableViewDelegate new];
    }
    return _delegate;
}

- (HYBookingRoomFilterView *)filterView{
    
    if (!_filterView) {
        _filterView = [HYBookingRoomFilterView new];
        _filterView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapFilterViewAction)];
        [_filterView addGestureRecognizer:tapGes];
        
        kWeakSelf(weakSelf);
        _filterView.SwitchActionBlock = ^(BOOL isOn) {
            
            weakSelf.status = isOn ? @"1": nil;
            [weakSelf requestNetwork];
        };
        
        _filterView.initSearchBlock = ^(NSDate *date, NSString *startTime, NSString *endTime,NSArray *cityBuildsList) {
            HYCityModel *cityModel = cityBuildsList.count == 0 ? [HYCityModel new] : cityBuildsList[0];
            HYBuildModel *buildModel = cityModel.builds.count == 0 ? [HYBuildModel new] : cityModel.builds[0];
            
            NSString *str = [date stringWithFormat:@"yyyy-MM-dd"];
            
            weakSelf.selectStartTime = [NSString stringWithFormat:@"%@ %@",str,startTime];
            weakSelf.selectEndTime = [NSString stringWithFormat:@"%@ %@",str,endTime];
            weakSelf.selectBuildingID = buildModel.id;
            weakSelf.selectCityID = cityModel.id;
            [weakSelf.dateCitySelectView initDefaultCityAndBuildingWithList:cityBuildsList];
            [weakSelf requestNetwork];
        };
    }
    return _filterView;
}

- (HYDateCitySelectView *)dateCitySelectView{
    
    if (!_dateCitySelectView) {
        
        _dateCitySelectView = [HYDateCitySelectView new];
        _dateCitySelectView.viewController = self;
        kWeakSelf(weakSelf);
        
        _dateCitySelectView.dateCitySearchBlock = ^(NSDate *date, NSString *startTime, NSString *endTime, NSString *cityID, NSString *buildingID, NSString *city, NSString *building) {
            
            DLog(@"date:%@,startTime:%@,endTime:%@,cityID:%@,buildingID:%@,city:%@,building:%@",date,startTime,endTime,cityID,buildingID,city,building);
            
            NSString *startTimeStr = [startTime substringToIndex:5];
            NSString *endTimeStr = [endTime substringToIndex:5];
            
            NSString *str = [date stringWithFormat:@"yyyy-MM-dd"];
            NSString *dateResultStr = [NSString stringWithFormat:@"%@ %@  %@ %@ ~ %@",city ?: @"",building ?: @"",str,startTimeStr,endTimeStr];
            weakSelf.filterView.dateStr = dateResultStr;
            
            weakSelf.selectStartTime = [NSString stringWithFormat:@"%@ %@",str,startTime];
            weakSelf.selectEndTime = [NSString stringWithFormat:@"%@ %@",str,endTime];
            weakSelf.selectBuildingID = buildingID;
            weakSelf.selectCityID = cityID;
            [weakSelf requestNetwork];
        };
    }
    return _dateCitySelectView;
}

- (UITableView *)tableView{
    
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = KAPP_TableView_BgColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self.delegate;
        _tableView.dataSource = self.dataSource;
        _tableView.emptyDataSetSource = self;
        _tableView.emptyDataSetDelegate = self;
        __weak typeof(self) weakSelf = self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakSelf.sectionArray removeAllObjects];
            [weakSelf.dataSetsArray removeAllObjects];
            [weakSelf requestNetwork];
        }];
    }
    return _tableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
